<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Article as ArticleModel;

class ArticleService
{
    /**
     * 文章统计
     * @param array $where
     * @return int
     */
    public static function countArticle($where = [])
    {
        return ArticleModel::countArticle($where);
    }

    /**
     * 本周文章数据（折线图）
     * @return mixed
     */
    public static function getArticleWeekLine()
    {
        $articleWeek = ArticleModel::getArticleWeek();
        return weekArr($articleWeek);
    }

    /**
     * 本月文章数据（折线图）
     * @param array $where
     * @return mixed
     */
    public static function getArticleMonthLine($where = [])
    {
        $startEndTime = getStartEndTime();
        $articleMonth = ArticleModel::getArticleMonth1($where, $startEndTime['startTime'], $startEndTime['endTime']);
        return inArr($articleMonth, $startEndTime['dateArr']);
    }

    /**
     * 本月新增文章
     * @param array $where
     * @return int
     */
    public static function countArticleMonthCreate($where = [])
    {
        return ArticleModel::countArticleMonthCreate($where);
    }

    /**
     * 文章列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleList($where, $page, $pagesize)
    {
        $data = ArticleModel::getArticleList($where, $page, $pagesize);
        foreach ($data as $k => $v) {
            // 查询阅读量
            $data[$k]['views'] = ArticleViewsService::countArticleViews(['id' => $v['id'], 'enroll_id' => $v['enroll_id']]);
            $data[$k]['cover'] = config('url.url') . $v['cover'];
            $data[$k]['cate_name'] = ArticleCateService::findArticleCateName(['id' => $v['cids']]);
            $data[$k]['type'] = $v['is_pay'] ? '付费' : '免费';
            $data[$k]['rid'] = $v['rid'] ?: '-';
            // 登记人
            $data[$k]['enroll_name'] = OfficialStaffService::findOfficialStaffValue(['id' => $v['enroll_id']], 'alias');
        }

        return $data;

    }

    /**
     * 文章列表统计
     * @param $where
     * @return int
     */
    public static function countArticleList($where)
    {
        return ArticleModel::countArticleList($where);
    }

    /**
     * 单文章数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findArticle($where)
    {
        return ArticleModel::findArticle($where);
    }

    /**
     * 更新文章数据
     * @param $data
     * @param $where
     * @return ArticleModel
     */
    public static function updateArticle($data, $where)
    {
        return ArticleModel::updateArticle($data, $where);
    }

    /**
     * 添加文章数据
     * @param $data
     * @return ArticleModel|\think\Model
     */
    public static function createArticle($data)
    {
        return ArticleModel::createArticle($data);
    }

    /**
     * 文章数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleData($where = [])
    {
        return ArticleModel::getArticleData($where);
    }
}